package org.example.model.Observable;

import org.example.other.SensorData;

import java.util.ArrayList;
import java.util.List;

public class EnvironmentSensor implements Observable {
    private List<Observer> observers = new ArrayList<>();
    private String sensorType;

    public EnvironmentSensor(String sensorType) {
        this.sensorType = sensorType;
    }

    public void setData(SensorData data) {
        System.out.println("\n[" + sensorType + "传感器] 检测到数据变化: " + data);
        notifyObservers(data);
    }

    public void addObserver(Observer o) {
        observers.add(o);
    }

    public void removeObserver(Observer o) {
        observers.remove(o);
    }

    public void notifyObservers(SensorData data) {
        for (Observer o : observers) {
            o.update(data);
        }
    }
}
